<?php
// TOOLS
include_once "application/tools/Authentification.php";

class AdminView {

    public function render() {
        $this->renderContent();
        $this->deconnect();
    }

    public function deconnect() {
        Authentification::doDeconnect();
    }

    public function renderContent() {
        $title = "Administration";
        $list = AdminController::loadAdmin("projects");
        $members = AdminController::loadAdmin("members");
        ?>
        <div id="page-content">

            <div id="admin-tab">
                <ul>
                    <?php if(Authentification::getCurrentMemberStatut() == Member::FULLADMIN)
                        echo '<li><a href="#admin-user">Utilisateurs</a></li>'; 
                    ?>
                    <li><a href="#admin-project">Projets</a></li>
                </ul>
                <?php if(Authentification::getCurrentMemberStatut() == Member::FULLADMIN) {
                    echo '
                <div id="admin-user">
                    <div id="admin-menu"></div>
                    <div class="DTTT_container">
                        <!--<a class="dTableButton dTableButton_text" id="user-add"><span>Nouveau</span></a>
                        <a class="dTableButton dTableButton_text DTTT_disabled" id="user-update"><span>Modifier</span></a>
                        <a class="dTableButton dTableButton_text DTTT_disabled" id="user-delete"><span>Supprimer</span></a>
                        -->
                        <button class="dTableButton dTableButton_text"  id="user-add"><span>Nouveau</span></button>
                        <button class="dTableButton dTableButton_text DTTT_disabled" disabled="disabled" id="user-update"><span>Modifier</span></button>
                        <button class="dTableButton dTableButton_text DTTT_disabled" disabled="disabled" id="user-delete"><span>Supprimer</span></button>
                    </div>
                    <div id="user-dialog">
                        <form id="user-add-form" name="user-add-form" method="POST">
                            <fieldset id="user-add-form-input">
                                <legend>Informations du compte</legend>
                                <label for="form-login">Login <em>*</em></label>
                                <input id="form-login" type="text"   required />
                                <span id="login-info">Login doit avoir au minimum 5 lettres.</span>  
                                <br />
                                <label for="form-password">Mot de passe <em>*</em></label>
                                <input id="form-password" type="password"  required/>
                                <br />
                                <label for="form-password-confirm">Confirmation Mot de passe <em>*</em></label>
                                <input id="form-password-confirm" type="password"  required/>
                                <span id="password-info"></span>
                                <br />
                                <label for="form-email">Email <em>*</em></label>
                                <input id="form-email" type="text"   required/>
                                <span id="mail-info"></span>
                                <br />
                                <label for="form-first-name">Pr&eacute;nom :</label>
                                <input id="form-first-name" type="text"  required/>
                                <br />
                                <label for="form-last-name">Nom :</label>
                                <input id="form-last-name" type="text"  required/>
                                <br />
                                <label for="form-phone">T&eacute;l&eacute;phone :</label>
                                <input id="form-phone" type="text"  required/>
                                <br />
                                <label for="form-address">Adresse :</label>
                                <input id="form-address" type="text"   required/>
                                <br />
                                <label for="form-city">Ville :</label>
                                <input id="form-city" type="text"  required/>
                                <br />
                                <label for="form-zip">Code Postal :</label>
                                <input id="form-zip" type="text"   required/>
                                <br />
                                <label for="form-company">Soci&eacute;t&eacute; :</label>
                                <input id="form-company" type="text"   required/>
                                <br />
                                <label for="form-status">Statut :</label>
                                <select id="form-status">
                                    <option value="0">Utilisateur</option>
                                    <option value="1">Administrateur global du site</option>
                                </select>

                                <br />
                                <label for="form-enabled">Inactif :</label>
                                <input id="form-enabled" type="checkbox" required/>
                                <br />
                            </fieldset>
                        </form>

                    </div>
                    <div id="user-dialog-update"></div>
                    <div id="user-dialog-delete">Etes vous sur de vouloir supprimer cet utilisateur ?</div>
                    <table cellpadding="0" cellspacing="0" border="0" class="display" id="dataTableUsers">
                        <thead>
                            <tr>
                                <th>Login</th>
                                <th>Nom-Pr&eacute;nom</th>
                                <th>email</th>
                                <th>Soci&eacute;t&eacute;</th>
                                <th>Adresse</th>
                                <th>Ville</th>
                                <th>Code postal</th>
                                <th>T&eacute;l&eacute;phone</th>
                                <th>Statut</th>
                                <th>Etat</th>
                            </tr>
                        </thead>
                        <tbody>';
                    
                            foreach ($members as $member) {
                                $grade = (($member->getIs_enabled() == 0) ? "gradeX" : "gradeB");
                                $status = $member->getStatut() == 0 ? 'Utilisateur' : 'Administrateur';
                                $state = $member->getIs_enabled() == 1 ? 'Actif' : 'Inactif';
                                echo "
                                    <tr class='" . $grade . "' id='member-" . $member->getId() . "'>

                                        <td>" . $member->getLogin() . "</td>
                                        <td>" . $member->getFirst_name() . " " . $member->getLast_name() . "</td>
                                        <td>" . $member->getEmail() . "</td>
                                        <td>" . $member->getCompany() . "</td>
                                        <td>" . $member->getAddress() . "</td>
                                        <td>" . $member->getCity() . "</td>
                                        <td>" . $member->getZip() . "</td>
                                        <td>" . $member->getPhone_number() . "</td>
                                        <td>" . $status . "</td>
                                        <td>" . $state . "</td>
                                    </tr>
                                ";
                            }
                            echo '
                        </tbody>
                    </table>
                </div> 
                    ';
                }
                ?>
                <div id="admin-project">
                    <div class="DTTT_container">
                        <?php if(Authentification::getCurrentMemberStatut() == Member::FULLADMIN)
                            echo '<button class="dTableButton dTableButton_text"  id="project-add"><span>Nouveau</span></button>'; 
                        ?>
                        <button class="dTableButton dTableButton_text DTTT_disabled" disabled="disabled" id="project-update"><span>Modifier</span></button>
                        <?php if(Authentification::getCurrentMemberStatut() == Member::FULLADMIN)
                            echo '<button class="dTableButton dTableButton_text DTTT_disabled" disabled="disabled" id="project-delete"><span>Supprimer</span></button>'; 
                        ?>

                    </div>
                    <div id="project-dialog">
                        <form id="project-add-form" name="project-add-form" enctype="multipart/form-data" method="POST" action="#&action=saveUser">
                            <fieldset id="project-add-form-input">
                                <legend>Informations sur le projet</legend>
                                <label for="form-projectname">Nom du projet :</label>
                                <input id="form-projectname" type="text"   required/>
                                <span id="projectname-info"></span> 
                                <br />
                                
                                <label for="form-projectcolumn">Liste des colonnes du fichier :</label>
                                <div id="project-column-div" class="ui-widget">
                                    <table id="project-column" class="ui-widget ui-widget-content">
                                        <thead>
                                            <tr class="ui-widget-header">
                                                <th>Colonne fichier CSV</th>
                                                <th>Nom de colonne</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <tr id="project-add-tr">
                                                <td><input type="text" id="column_csv" name="colum_csv"/></td>
                                                <td><input type="text" id="column_name" name="colum_name"/></td>
                                                <td><button type="button" id="project-add-row">Add</button></td>
                                            </tr>
                                            <tr>
                                                <td class="csv">station_id</td>
                                                <td>Numéro de poste</td>

                                            </tr>
                                            <tr>
                                                <td class="csv">dat</td>
                                                <td>Date</td>
                                            </tr>

                                        </tbody>
                                    </table>
                                </div>
                                <br />
                                <label for="form-projectnblign">Nombre de points affichés :</label>
                                <input id="form-projectnblign" type="text" value="30"  required/>
                                <br />
                                <label for="_checkin_pic">Image du projet :</label>
                          		<span id="form-upcheckin">
								<input type="file" name="_checkin_pic" id="checkin_pic">
                                                </span>
								<!-- <a href="#" id="bpci-polaroid-upload" title="Attach image"><span>Attach image</span></a> -->
                                <br />
                                <label for="form-list-admin-2">Liste des mod&eacute;rateurs : </label>
                                <select id ="form-list-admin-2" data-placeholder="Choissisez les administrateurs" multiple="multiple">
                                    <?php
                                        foreach ($members as $member) {
                                            echo '<option value="' . $member->getId() . '">' . $member->getLogin() . '</option>';
                                        }
                                    ?>
                                </select>
                                <span id="projectcolumn-info">Un mod&eacute;rateur ne peut &ecirc;tre un utilisateur.</span>  
                                <br/>
                                <br/>
                                <label for="form-list-user">Liste des utilisateurs :</label>
                                <select id="form-list-user" multiple="multiple"><?php
                                    foreach ($members as $member) {
                                        echo '<option value="' . $member->getId() . '">' . $member->getLogin() . '</option>';
                                    }
                                ?>
                                </select>
                                <br />
                                
                                <label for="form-default-graphe">Graphe par d&eacute;faut :</label>
                                <select id="form-default-graphe" class="jquery-ui-themeswitcher-trigger">
                                    <option value="<?php echo Project::GRAPHE_COURBE; ?>">Courbe</option>
                                    <option value="<?php echo Project::GRAPHE_POINT; ?>">Nuage de points</option>
                                    <option value="<?php echo Project::GRAPHE_HIST0; ?>">Histogramme</option>
                                    <option value="<?php echo Project::GRAPHE_SECTEUR; ?>">Barre de Secteur</option>
                                </select>

                                <br />
                            </fieldset>
                        </form>
                    </div>
                    <table cellpadding="0" cellspacing="0" border="0" class="display" id="dataTableProjects">
                        <thead>
                            <tr>
                                <th>Projet</th>
                                <th>Modérateur(s)</th>
								<th>utilisateur(s)</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            for ($i = 0; $i < sizeof($list); $i++) { // tant que $i est inferieur au nombre d'éléments du tableau... 
                                $stringAdmin = "";
                                $stringUser = "";
                                $listadmin = $list[$i]->getAdminProject();
                                $listusers = $list[$i]->getUserProject();
                                for ($j = 0; $j < sizeof($listadmin); $j++) { // tant que $i est inferieur au nombre d'éléments du tableau...
                                    $stringAdmin = $stringAdmin . $listadmin[$j];
                                    if ($j < sizeof($listadmin) - 1) {
                                        $stringAdmin = $stringAdmin . ", ";
                                    }
                                }
                                for ($j = 0; $j < sizeof($listusers); $j++) { // tant que $i est inferieur au nombre d'éléments du tableau...
                                	$stringUser = $stringUser . $listusers[$j];
                                	if ($j < sizeof($listusers) - 1) {
                                		$stringUser = $stringUser . ", ";
                                	}
                                }
                                echo "<tr id='project-" . $list[$i]->getId() . "'>
		                                            <td>" . $list[$i]->getProjectName() . "</td>
		                                            <td>" . $stringAdmin . "</td>
		                                            <td>" . $stringUser . "</td>
                                    </tr>";
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
                <div id="project-dialog-update"></div>
                <div id="project-dialog-delete">Etes vous sur de vouloir supprimer ce projet ?</div>

            </div>
        </div>
</div>
        <!-- END CONTENT -->

        <?php
    }

}
?>
